package com.example.view;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.app.MyApplication;
import com.example.netity.Deviceinfo;
import com.example.netity.Shike;
import com.example.tool.BaseActivity;
import com.example.tool.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

public class DeviceSettingActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_device_setting);
        initView();
        mQueue = MyApplication.getRequestQueue();
        getDeviceinfo();
    }

    private ImageView title_left_btn;
    private TextView title_tv;
    private TextView tv_right_btn;

    private void initView() {
        title_left_btn = (ImageView) findViewById(R.id.title_left_btn);
        title_left_btn.setVisibility(View.VISIBLE);
        title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText(R.string.devicesetting);
        tv_right_btn = (TextView) findViewById(R.id.tv_right_btn);
        tv_right_btn.setText(R.string.save);
        title_left_btn.setOnClickListener(this);
        tv_right_btn.setOnClickListener(this);

        findViewById(R.id.tv_timeperiod_0_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_0_end).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_1_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_1_end).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_2_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_2_end).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_3_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_3_end).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_4_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_4_end).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_5_start).setOnClickListener(this);
        findViewById(R.id.tv_timeperiod_5_end).setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.tv_right_btn:
                onSave();
                break;
            case R.id.tv_timeperiod_0_start:
                onTimeperiodStart(0);
                break;
            case R.id.tv_timeperiod_1_start:
                onTimeperiodStart(1);
                break;
            case R.id.tv_timeperiod_2_start:
                onTimeperiodStart(2);
                break;
            case R.id.tv_timeperiod_3_start:
                onTimeperiodStart(3);
                break;
            case R.id.tv_timeperiod_4_start:
                onTimeperiodStart(4);
                break;
            case R.id.tv_timeperiod_5_start:
                onTimeperiodStart(5);
                break;
            case R.id.tv_timeperiod_0_end:
                onTimeperiodEnd(0);
                break;
            case R.id.tv_timeperiod_1_end:
                onTimeperiodEnd(1);
                break;
            case R.id.tv_timeperiod_2_end:
                onTimeperiodEnd(2);
                break;
            case R.id.tv_timeperiod_3_end:
                onTimeperiodEnd(3);
                break;
            case R.id.tv_timeperiod_4_end:
                onTimeperiodEnd(4);
                break;
            case R.id.tv_timeperiod_5_end:
                onTimeperiodEnd(5);
                break;
            default:
                break;
        }
    }

    Deviceinfo _deviceinfo = new Deviceinfo();

    private void onTimeperiodStart(final int index) {

        Shike sk = _deviceinfo.getTimeperiodStart(index);
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                _deviceinfo.setTimeperiodStart(index, hourOfDay, minute);
                refresh();
            }
        }, sk.hour, sk.miniute, true).show();
    }

    private void onTimeperiodEnd(final int index) {
        Shike sk = _deviceinfo.getTimeperiodEnd(index);
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                _deviceinfo.setTimeperiodEnd(index, hourOfDay, minute);
                refresh();
            }
        }, sk.hour, sk.miniute, true).show();
    }

    private void onSave() {
        _deviceinfo.pname = ((EditText) findViewById(R.id.et_chanxianhao)).getText().toString();
        _deviceinfo.renshu = Integer.valueOf(((EditText) findViewById(R.id.et_renshu)).getText().toString());
        _deviceinfo.jiepai = Integer.valueOf(((EditText) findViewById(R.id.et_jiepai)).getText().toString());
        _deviceinfo.taitou = ((EditText) findViewById(R.id.et_kbtt)).getText().toString();
        postDeviceinfo();
    }

    void refresh() {
        ((TextView) findViewById(R.id.tv_timeperiod_0_start)).setText(_deviceinfo.k1);
        ((TextView) findViewById(R.id.tv_timeperiod_0_end)).setText(_deviceinfo.j1);
        ((TextView) findViewById(R.id.tv_timeperiod_1_start)).setText(_deviceinfo.k2);
        ((TextView) findViewById(R.id.tv_timeperiod_1_end)).setText(_deviceinfo.j2);
        ((TextView) findViewById(R.id.tv_timeperiod_2_start)).setText(_deviceinfo.k1_2);
        ((TextView) findViewById(R.id.tv_timeperiod_2_end)).setText(_deviceinfo.j1_2);
        ((TextView) findViewById(R.id.tv_timeperiod_3_start)).setText(_deviceinfo.k2_2);
        ((TextView) findViewById(R.id.tv_timeperiod_3_end)).setText(_deviceinfo.j2_2);
        ((TextView) findViewById(R.id.tv_timeperiod_4_start)).setText(_deviceinfo.k1_3);
        ((TextView) findViewById(R.id.tv_timeperiod_4_end)).setText(_deviceinfo.j1_3);
        ((TextView) findViewById(R.id.tv_timeperiod_5_start)).setText(_deviceinfo.k2_3);
        ((TextView) findViewById(R.id.tv_timeperiod_5_end)).setText(_deviceinfo.j2_3);

        ((EditText) findViewById(R.id.et_chanxianhao)).setText(_deviceinfo.pname);
        ((EditText) findViewById(R.id.et_renshu)).setText(_deviceinfo.renshu + "");
        ((EditText) findViewById(R.id.et_jiepai)).setText(_deviceinfo.jiepai + "");
        ((EditText) findViewById(R.id.et_kbtt)).setText(_deviceinfo.taitou);
        ((TextView) findViewById(R.id.tv_mbcl)).setText(_deviceinfo.mbcl + "");
        ((TextView) findViewById(R.id.tv_mbpph)).setText(String.format("%.2f", (float)_deviceinfo.mbpph));

    }

    private String _deviceurl = MyApplication.SERVERPATH + "/Y502/Deviceinfo";
    RequestQueue mQueue = null;
    JsonObjectRequest mJsonObjectRequest = null;

    private void getDeviceinfo() {
        try {
            boolean isConnected = Utils.toastIsNetworkConnected(context);
            if (isConnected) {
                showProgressBar();
                mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, _deviceurl,
                        null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        hideProgressBar();
                        parseData(response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        hideProgressBar();
                        showToast(getString(R.string.network_error_try_again));
                    }
                }
                );
                mQueue.add(mJsonObjectRequest);
            } else {
                showToast(getString(R.string.network_error_try_again));
                return;
            }
        } catch (Exception e) {
            hideProgressBar();
            showToast(getString(R.string.network_error_try_again));
        }

    }

    public void parseData(JSONObject response) {
        String result = response.optString("result");
        if (result == null || result.equals("") || result.equals("fail")) {
            return;
        }

        try {
            JSONArray array = response.optJSONArray("data");
            if (array != null) {
                JSONObject obj = array.optJSONObject(0);
                _deviceinfo.pname = obj.optString("linename");
                _deviceinfo.renshu = Integer.valueOf(obj.optString("renshu"));
                _deviceinfo.jiepai = Integer.valueOf(obj.optString("jiepai"));
                _deviceinfo.taitou = obj.optString("taitou");
                _deviceinfo.mbcl = Integer.valueOf(obj.optString("mbcl"));
                _deviceinfo.mbpph = Float.valueOf(obj.optString("mbpph"));

                _deviceinfo.k1 = obj.optString("k1");
                _deviceinfo.k2 = obj.optString("k2");
                _deviceinfo.j1 = obj.optString("j1");
                _deviceinfo.j2 = obj.optString("j2");
                _deviceinfo.k1_2 = obj.optString("k1_2");
                _deviceinfo.k2_2 = obj.optString("k2_2");
                _deviceinfo.j1_2 = obj.optString("j1_2");
                _deviceinfo.j2_2 = obj.optString("j2_2");
                _deviceinfo.k1_3 = obj.optString("k1_3");
                _deviceinfo.k2_3 = obj.optString("k2_3");
                _deviceinfo.j1_3 = obj.optString("j1_3");
                _deviceinfo.j2_3 = obj.optString("j2_3");
            }
            refresh();

        } catch (Exception e) {

        }

    }

    private String _deviceposturl = MyApplication.SERVERPATH + "/Y502/Setdeviceinfo";
    private void postDeviceinfo() {
        try {
            boolean isConnected = Utils.toastIsNetworkConnected(context);
            if (isConnected) {
                showProgressBar();
                String uu = _deviceposturl + "?pname=" + _deviceinfo.pname + "&&renshu=" + _deviceinfo.renshu + "&&jiepai="
                        + _deviceinfo.jiepai + "&&taitou=" + _deviceinfo.taitou
                        + "&&k1=" + _deviceinfo.k1 + "&&k2=" + _deviceinfo.k2 + "&&j1=" + _deviceinfo.j1 + "&&j2=" + _deviceinfo.j2
                        + "&&k1_2=" + _deviceinfo.k1_2 + "&&k2_2=" + _deviceinfo.k2_2 + "&&j1_2=" + _deviceinfo.j1_2 + "&&j2_2=" + _deviceinfo.j2_2
                        + "&&k1_3=" + _deviceinfo.k1_3 + "&&k2_3=" + _deviceinfo.k2_3 + "&&j1_3=" + _deviceinfo.j1_3 + "&&j2_3=" + _deviceinfo.j2_3 ;
                mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                        null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        hideProgressBar();
                        finish();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        hideProgressBar();
                        showToast(getString(R.string.network_error_try_again));
                    }
                }
                );
                mQueue.add(mJsonObjectRequest);
            } else {
                showToast(getString(R.string.network_error_try_again));
                return;
            }
        } catch (Exception e) {
            hideProgressBar();
            showToast(getString(R.string.network_error_try_again));
        }
    }

}
